package combat;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;

import javax.swing.Timer;

import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;

import custom.Clock;

public class Resistance implements ActionListener {
	private static final PotionEffectType RESISTANCE = PotionEffectType.DAMAGE_RESISTANCE;
	private final LivingEntity target;
	private final Timer timer;
	private Clock buffClock, debuffClock, appleClock;
	private int stacks, buff, godApple;
	private static HashMap<LivingEntity, Resistance> resistances = new HashMap<LivingEntity, Resistance>();

	public Resistance(LivingEntity target) {
		this.target = target;
		stacks = 0;
		buff = 0;
		godApple = 0;

		timer = new Timer(10000, this);
		timer.setRepeats(false);
		timer.start();
		buffClock = new Clock();
		debuffClock = new Clock();
		appleClock = new Clock();
		
		resistances.put(target, this);
	}

	@Override
	public void actionPerformed(ActionEvent event) {
		if (buffClock.getTime(Clock.SECONDS) < 10) {
			int duration = (int) (200 - buffClock.getTicks());
			PotionEffect resistance = new PotionEffect(RESISTANCE, duration, 0);
			target.addPotionEffect(resistance);
		}
		else if (debuffClock.getTime(Clock.SECONDS) < 10) {
			int duration = (int) (200 - debuffClock.getTicks());
			PotionEffect resistance = new PotionEffect(RESISTANCE, duration, stacks);
			target.addPotionEffect(resistance);
		}
		else {
			if (godApple - appleClock.getTicks() > 0) {
				PotionEffect effect = new PotionEffect(RESISTANCE, (int) (godApple - appleClock.getTicks()), 0);
				((Player) target).sendMessage("" + effect.getDuration());
				target.addPotionEffect(effect);
			}			
		}
		resistances.remove(target);
	}
	
	private PotionEffect getEffect() {
		switch (stacks + buff + godApple > 0? 1 : 0) {
		case 1:
			return new PotionEffect(RESISTANCE, 200, 0);
		case 0:
			return new PotionEffect(RESISTANCE, 0, 0);
		default:
			return new PotionEffect(RESISTANCE, 200, stacks);
		}
	}
	
	private void godApple() {
		appleClock = new Clock();
		for(PotionEffect effect : target.getActivePotionEffects()) {
			if (effect.getType().equals(RESISTANCE)) {
				if (effect.getAmplifier() == 0) {
					if (effect.getDuration() > 200) {
						godApple = effect.getDuration();
					}
				}
			}
		}
	}
	
	
	private void debuff() {
		godApple();
		stacks--;
		timer.restart();
		debuffClock.reset();
		PotionEffect resistance = this.getEffect();
		if (resistance.getAmplifier() < 0) {
			if (target instanceof Player) {
				Player player = (Player) target;
				int strength = Math.max(resistance.getAmplifier(), -5) * -20;
				player.sendMessage("Stacks: " + stacks);
				player.sendMessage("You take " + strength + "% increased damage!");
			}
			target.addPotionEffect(resistance, true);
		}		
	}
	
	private void buff() {
		godApple();
		buff = 1;
		timer.restart();
		buffClock.reset();
		PotionEffect resistance = this.getEffect();
		if (resistance.getDuration() > 0 && resistance.getAmplifier() == 0) {
			if (target instanceof Player) {
				Player player = (Player) target;
				player.sendMessage("You take 20% reduced damage!");
			}
			target.addPotionEffect(resistance);
		}
	}

	public static void debuff(LivingEntity target) {
		if (resistances.containsKey(target) == false) {
			resistances.put(target, new Resistance(target));
		}		
		resistances.get(target).debuff();
	}

	public static void buff(LivingEntity target) {
		if (resistances.containsKey(target) == false) {
			resistances.put(target, new Resistance(target));
		}
		resistances.get(target).buff();
	}
}
